home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 September / CHIP Eylül 1998.iso / Slackwar / docs / mini / Clock < prev    next >
Text File  |  1997-08-07  |  11KB  |  243 lines

  1.   The Clock Mini-HOWTO
  2.   Ron Bean, rbean@execpc.com
  3.   Dec. 1996
  4.  
  5.   How to set and keep your computer clock on time.
  6.  
  7.   1.  Introduction
  8.  
  9.   The real-time-clock chips used on PC motherboards (and even expensive
  10.   workstations) are notoriously inaccurate. Linux provides a simple way
  11.   to correct for this in software, making the clock potentially *very*
  12.   accurate even without an external time source. But most people don't
  13.   seem to know about it, for several reasons:
  14.  
  15.   1. It's not mentioned in most of the general ``how to set up linux''
  16.      documentation, and it would be difficult to set up automatically at
  17.      install time (although not impossible in theory, if you have a
  18.      modem).
  19.  
  20.   2. If you check ``man clock'' you'll get clock(3), which is not what
  21.      you want. (try ``man 8 clock'').
  22.  
  23.   3. Most people don't seem to care what time it is anyway.
  24.  
  25.   4. Those few who do care often want to use the xntpd package from
  26.      louie.udel.edu to sync to an external time source, such as a
  27.      network time server or radio clock.
  28.  
  29.   This mini-HOWTO describes the low-tech approach.  If you're at all
  30.   interested in this sort of thing, I highly recommend that you spend
  31.   some time at <http://www.eecis.udel.edu/~ntp/> which includes all
  32.   kinds of interesting stuff, including complete info on xntpd and links
  33.   to NIST and USNO (I have a few more comments on xntpd at the end.)
  34.  
  35.      Note
  36.         If you run more than one OS on your machine, you should only let
  37.         one of them reset the CMOS clock, so they don't confuse each
  38.         other.  If you regularly run both Linux and Windows on the same
  39.         machine, you may want to check out some of the shareware clock
  40.         programs that are available for Windows instead (follow the
  41.         links from the URL above).
  42.  
  43.   2.  Using the ``clock'' program
  44.  
  45.   Everything you need to know is in the clock(8) man page, but this
  46.   mini-HOWTO will walk you through the process.
  47.  
  48.      Note
  49.         You must be root to run ``clock'', or any other program that
  50.         affects either the system time or the CMOS clock.
  51.  
  52.   2.1.  Checking your installation
  53.  
  54.   Check your system startup files for a command like ``clock -a'' or
  55.   ``clock -ua''.  Depending on which distribution you're using, it might
  56.   be in /etc/rc.local, or /etc/rc.d/rc.sysinit, or some similar place.
  57.  
  58.   If it says ``clock -s'' or ``clock -us'', change the ``s'' to an
  59.   ``a'', and then check to see if you have the file /etc/adjtime, which
  60.   contains a single line that looks something like this:
  61.  
  62.        0.000000 842214901 0.000000
  63.  
  64.   These numbers are the correction factor (in seconds per day), the time
  65.   the clock was last corrected (in seconds since Jan 1, 1970), and the
  66.   partial second that was rounded off last time.  If you don't have this
  67.   file, login as root and create it, with a single line that looks like
  68.   this (all zeros):
  69.  
  70.        0.0 0 0.0
  71.  
  72.   Then run ``clock -a'' or ``clock -ua'' manually from the shell to
  73.   update the 2nd number (use the ``u'' if your clock is set to Universal
  74.   instead of local time).
  75.  
  76.   2.2.  Measuring your clock's drift rate
  77.  
  78.   First, you need to know what time it is :-).  Your local time of day
  79.   number may or may not be accurate.  My favorite method is to call
  80.   WWV's voice announcment at (303)499-7111 (this is a toll call).  If
  81.   you have access to a network time server, you can use the ntpdate
  82.   program from the xntpd package (use the -b flag to keep the kernel
  83.   from messing with the CMOS clock).  Otherwise use ``date -s hh:mm:ss''
  84.   to set the kernel time by hand, and then ``clock -w'' to set the CMOS
  85.   clock from the kernel clock.  You'll need to remember when you last
  86.   set the clock, so write down the date someplace where you won't lose
  87.   it.  If you used ntpdate, do ``date +%s'' and write down the number of
  88.   seconds since Jan 1,1970.
  89.  
  90.   Then come back some days or weeks later and see how far the clock has
  91.   drifted.  If you're setting the clock by hand, I'd recommend waiting
  92.   at least two weeks, and only calculate the drift rate to the nearest
  93.   .1 sec/day.  After several months you could get to the nearest .01
  94.   sec/day (some people claim more accuracy than that, but I'm being
  95.   conservative here).  If you use ntpdate you shouldn't have to wait
  96.   that long, but in any case you can always fine-tune it later.
  97.  
  98.   You can have cron run ``clock -a'' at regular intervals to keep the
  99.   system time in line with the (corrected) CMOS time.  This command will
  100.   also be run from your startup file every time you boot the system, so
  101.   if you do that often (as some of us do), that may be enough for your
  102.   purposes.
  103.  
  104.   Note that certain programs may complain if the system time jumps by
  105.   more than one second at a time, or if it jumps backwards.  If you have
  106.   this problem, you can use xntpd or ntpdate to correct the time more
  107.   gradually.
  108.  
  109.   2.3.  Example
  110.  
  111.   2.3.1.  To set time
  112.  
  113.   Login as root.  Dial (303)499-7111 (voice), listen for time
  114.   announcement.  Then type:
  115.  
  116.        date -s hh:mm:ss
  117.  
  118.   but don't press enter until you hear the beep.  (You could use ``ntp¡
  119.   date'' here instead of ``date'', and skip the phone call) This sets
  120.   the ``kernel time''.  Then type:
  121.  
  122.   clock -w
  123.  
  124.   This sets the CMOS time to match the kernel time.  Then type:
  125.  
  126.        date +%j
  127.  
  128.   (or ``date +%s'' if you used ``ntpdate'' instead of ``date'' above)
  129.   and write down the number it gives you for next time.
  130.  
  131.   2.3.2.  To reset time and check drift rate
  132.  
  133.   Find the date you wrote down last time.  Login as root Then type:
  134.  
  135.        clock -a
  136.  
  137.   This sets the kernel time to match the current CMOS time.  Dial
  138.   (303)499-7111 (voice), listen for announcement.  Then type:
  139.  
  140.        date
  141.  
  142.   and press enter when you hear the beep, but while you're waiting,
  143.   write down the time they announce, and don't hang up yet.  This tells
  144.   you what time your machine thought it was, when it should have been
  145.   exactly on the minute.  Now type in
  146.  
  147.        date hh:mm:00
  148.  
  149.   using the minute *after* the one that was just announced, and press
  150.   enter when you hear the beep again (now you can hang up).  For hh use
  151.   the local hour.  This sets the ``kernel time''.  Then type:
  152.  
  153.        clock -w
  154.  
  155.   which writes the new (correct) time to the CMOS clock.  Now type:
  156.  
  157.        date +%j
  158.  
  159.   (or ``date +%s'' if that's what you used before)
  160.  
  161.   You now have three numbers (two dates and a time) that will allow you
  162.   to calculate the drift rate.
  163.  
  164.   2.3.3.  Calculating the correction factor
  165.  
  166.   When you ran ``date'' on the minute, was your machine slow or fast?
  167.   If it was fast, you'll have to subtract some number of seconds, so
  168.   write it down as a negative number.  If it was slow, you have to add
  169.   some seconds, so write it down as positive.
  170.  
  171.   Now subtract the two dates.  If you used ``date +%j'', the numbers
  172.   represent the day-of-year (1-365, or 1-366 in leap years).  If you've
  173.   passed Jan 1 since you last set the clock you'll have to add 365 (or
  174.   366) to the 2nd number.  If you used ``date +%s'' then your number is
  175.   in seconds, and you'll have to divide it by 86400 to get days.
  176.  
  177.   If you already had a correction factor in /etc/adjtime, you'll have to
  178.   account for the number of seconds you've already corrected.  If you've
  179.   overcorrected, this number will have the opposite sign of the one you
  180.   just measured; if you've undercorrected it will have the same sign.
  181.   Multiply the old correction factor by the number of days, and then add
  182.   the new number of seconds (signed addition-- if the two numbers have
  183.   the same sign, you'll get a larger number, if they have opposite
  184.   signs, you'll get a smaller number).
  185.  
  186.   Then divide the total number of seconds by the number of days to get
  187.   the new correction factor, and put it in /etc/adjtime in place of the
  188.   old one.  Write down the new date (in seconds or days) for next time.
  189.  
  190.   Here's what my /etc/adjtime looks like:
  191.  
  192.        -9.600000 845082716 -0.250655
  193.  
  194.   (note 9.6 seconds per day is nearly five minutes per month!)
  195.  
  196.   3.  A few words about xntpd
  197.  
  198.   Your system actually has two clocks-- the battery powered ``real time
  199.   clock'' that keeps track of time when the system is turned off (also
  200.   known as the ``CMOS clock'', ``Hardware clock'', or ``RTC'') and the
  201.   ``kernel time'' (sometimes called the ``software clock'' or ``system
  202.   clock'') which is based on the timer interrupt and is initialized from
  203.   the CMOS clock at boot time.  The two will drift at different rates,
  204.   so they will gradually drift apart from each other, and also away from
  205.   the ``real'' time.
  206.  
  207.   All references to ``the clock'' in the xntpd documentation refer to
  208.   the ``kernel clock''.  When you run xntpd or timed (or any other
  209.   program that uses the adjtimex system call), the linux kernel assumes
  210.   that the kernel clock is more accurate than the CMOS clock, and resets
  211.   the CMOS time every 11 minutes from then on (until you reboot the
  212.   machine).  This means that ``clock'' no longer knows when the CMOS
  213.   clock was last reset, so you can't use the correction factor in
  214.   /etc/adjtime.  You can use ntpdate in your startup file to initially
  215.   set the clock from a timeserver before starting xntpd.  If you don't
  216.   always have access to an accurate time source when you boot the
  217.   machine, this can be a bit awkward-- xntpd isn't really designed to be
  218.   used in situations like that.
  219.  
  220.   Xntpd includes drivers for many radio clocks, and can also be set to
  221.   call NIST's dial-up time service at regular intervals (be sure to
  222.   calculate the effect on your phone bill when setting the interval
  223.   between calls).  It can also apply a correction factor to the kernel
  224.   clock if it loses contact with its other sources for an extended
  225.   period of time.
  226.  
  227.   Most radio clocks cost $3-4K, but you can get plans for an inexpensive
  228.   ``gadget box'' (actually a 300 baud modem) that sits between your
  229.   computer and any shortwave radio tuned to Canada's CHU time station
  230.   (see  <ftp://ftp.udel.edu/pub/ntp/gadget.tar.Z>).  The Heathkit WWV
  231.   receiver (the ``Most Accurate Clock'') is also still available
  232.   (although not as a kit), and costs around $4-500.  GPS signals also
  233.   contain time information, and some GPS recievers can connect to a
  234.   serial port.  This may become the low cost solution in the near
  235.   future.
  236.  
  237.   In theory, someone could write a program to use NIST's dial up time
  238.   service to calculate the drift rate of both the CMOS clock and the
  239.   kernel clock automatically.  I am not aware of any stand-alone program
  240.   to do this, but most of the code could probably be borrowed from
  241.   xntpd.
  242.  
  243.